﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.HardWareHelp;

namespace ZK.YCGLRD_TransManage.UC
{
    /// <summary>
    /// 门卫室打印榜单
    /// </summary>
    public partial class PrintPoundList : UserControl
    {
        /// <summary>
        /// 室外读卡器
        /// </summary>
        private HardWareHelp.MW_102N_CardReader mW_102N_CardReader;
        /// <summary>
        /// 运销车辆信息
        /// </summary>
        private Models.YxYsTransportReturnDto yxYsTransportReturnDto;
        private WFAutoSizeHelper wFAutoSizeHelper = new WFAutoSizeHelper();
        Models.ScalesPrint scalesPrint = new Models.ScalesPrint();
        PrintPreviewDialog printPriview = new PrintPreviewDialog();

        private string nowCardNum = "";
        public PrintPoundList()
        {
            InitializeComponent();
            yxYsTransportReturnDto = new Models.YxYsTransportReturnDto();
            Common.DelegateHelper.MW_102N_MessageReceivedEvent += MW_102N_MessageReceived;

            wFAutoSizeHelper.GetAllInitInfo(this.Controls[0], this);
            Common.DelegateHelper.SendWidthHeightEvent += SendWidthHeight;

            InitSWReader();
        }



        /// <summary>
        /// 室外读卡器初始化
        /// </summary>
        private void InitSWReader()
        {
            mW_102N_CardReader = new HardWareHelp.MW_102N_CardReader("MW");
        }

        /// <summary>
        /// 串口变化委托事件
        /// </summary>
        /// <param name="Width"></param>
        /// <param name="height"></param>
        private void SendWidthHeight(int Width, int height)
        {
            if (wFAutoSizeHelper.ControlsInfo.Count > 0)//如果字典中有数据，即窗体改变
            {
                wFAutoSizeHelper.ControlsChangeInit(Width, height);//表示pannel控件
                wFAutoSizeHelper.ControlsChange(this.Controls[0]);
            }
        }

        /// <summary>
        /// 读卡器委托事件
        /// </summary>
        /// <param name="cardNum"></param>
        private void MW_102N_MessageReceived(string cardNum)
        {
            if (nowCardNum != cardNum)
            {
                nowCardNum = cardNum;
                try
                {
                    this.Invoke(new Action(() =>
                    {
                        if (textBoxCardNum.Text.Trim() != cardNum)
                        {
                            textBoxCardNum.Text = cardNum;
                        }

                    }));
                    //生成榜单接口
                    {
                        var resMakePoundList = Com.WebApiService.TruckWeightBill(
                          new Models.WeightBillDtoSendM()
                          {
                              cardNo = cardNum,
                              truckNo = "",
                          }
                          );
                        if (!resMakePoundList.success)
                        {
                            DialogNowMessageShow(resMakePoundList.msg);
                        }
                    }
                    //结束运单接口
                    {
                        var resEnd = Com.WebApiService.EndYxInfo(cardNum);
                        if (!resEnd.success)
                        {
                            DialogNowMessageShow(resEnd.msg);
                        }
                    }
                    //查询最后一条运单接口 拿到数据显示在页面上
                    {
                        var getYxInfoNewRes = Com.WebApiService.GetTheLatest(cardNum);
                        if (!getYxInfoNewRes.success)
                        {
                            DialogNowMessageShow(getYxInfoNewRes.msg);
                        }
                        if (getYxInfoNewRes.data == null)
                        {
                            DialogNowMessageShow("数据为空！");
                        }
                        else
                        {
                            yxYsTransportReturnDto = getYxInfoNewRes.data;
                            SetText();
                        }


                    }
                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr("打印磅单页面 MW_102N_MessageReceived " + ex.ToString());
                    DialogNowMessageShow(ex.Message);

                }
            }

        }

        /// <summary>
        /// 设置页面数据
        /// </summary>
        private void SetText()
        {
            this.Invoke(new Action(() =>
            {
                textBoxKZT.Text = Enum.GetName(typeof(SysDefinitionEnum.SendType), yxYsTransportReturnDto.sendType);
                textBoxCYS.Text = yxYsTransportReturnDto.carrierName;
                textBoxGSM.Text = yxYsTransportReturnDto.shipperName;
                textBoxHWMC.Text = yxYsTransportReturnDto.cargoName;
                textBoxBZ.Text = yxYsTransportReturnDto.memo;
                textBoxLX.Text = Enum.GetName(typeof(SysDefinitionEnum.TruckState), yxYsTransportReturnDto.truckState);
            }));
        }

        /// <summary>
        /// 清空页面数据
        /// </summary>
        private void ClearText()
        {
            this.Invoke(new Action(() =>
            {
                textBoxKZT.Text = "";
                textBoxCYS.Text = "";
                textBoxGSM.Text = "";
                textBoxHWMC.Text = "";
                textBoxBZ.Text = "";
                textBoxLX.Text = "";
                textBoxCardNum.Text = "";
            }));
            nowCardNum = "";
        }

        /// <summary>
        /// 判断是否打印预览，并关闭打印页面。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Document_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            System.Drawing.Printing.PrintDocument printDocument = sender as System.Drawing.Printing.PrintDocument;
            bool printRes = printDocument.PrintController.IsPreview;
            if (!printRes)
            {
                printPriview.Close();
            }
        }

        /// <summary>
        /// 清空按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonClear_Click(object sender, EventArgs e)
        {
            ClearText();
        }

        /// <summary>
        /// 提示信息显示在页面上，弹窗显示。
        /// </summary>
        /// <param name="str"></param>
        private void DialogNowMessageShow(string str)
        {
            NowMessagesShow(str);
            // MessageDialog.MessageBoxShow(str, "操作错误");
        }

        /// <summary>
        /// 提示信息显示在页面上
        /// </summary>
        /// <param name="str"></param>
        private void NowMessagesShow(string str)
        {

            labelNowMessage.Text = str;
        }

        /// <summary>
        /// 打印预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonPrint_Click(object sender, EventArgs e)
        {
            //string cardNum = textBoxCardNum.Text.Trim();
            ////获取运单信息
            //var resGetInfoM = Com.WebApiService.GetTheLatest(cardNum);
            //if (!resGetInfoM.success)
            //{
            //    DialogNowMessageShow(resGetInfoM.msg);
            //    return;
            //}
            //if (resGetInfoM.data == null)
            //{
            //    DialogNowMessageShow("数据为空！");
            //    return;
            //}
            //yxYsTransportReturnDto = resGetInfoM.data;
            //打印预览
            scalesPrint.yxYsTransportReturnDto = yxYsTransportReturnDto;
            printPriview = new PrintPreviewDialog
            {
                Document = scalesPrint.printDocument(Models.ScalesPrint.ScalesType.one),
                WindowState = FormWindowState.Maximized,
            };
            printPriview.Document.BeginPrint += Document_BeginPrint;
            //printPriview.ShowDialog();//预览
            //printPriview.Document.Print();//直接打印
            ClearText();
        }
    }
}
